import { downByBlob } from '@/utils/file/downFile';
import api from '@/api';
import { unref } from 'vue';
import { message } from 'ant-design-vue';

export const useTableToolbar = (searchQuery, paginationConfig) => {
    const exportAllFile = async (type: 0 | 1): Promise<void> => {
        let request: any = null;
        let fileName = '';
        switch (type) {
            case 0:
                request = api.pmpRequestInfoModel.exportAllFile;
                fileName = '业务需求清单.xls';
                break;
            case 1:
                request = api.productRequireModel.exportAllFile;
                fileName = '产品需求清单.xls';
                break;
        }
        const { current: pageNum } = unref(paginationConfig);
        const query = {
            ...unref(searchQuery),
            pageNum,
            pageSize: 0
        };

        const hide = message.loading('正在导出数据，请稍候...', 0);
        try {
            const res = await request(query, { responseType: 'blob' });
            if (res) {
                downByBlob(res, fileName);
                message.success('导出成功');
            }
        } catch (error) {
            message.error('导出失败，请重试');
        } finally {
            hide();
        }
    };
    return {
        exportAllFile
    };
};
